package com.example.bakend.controller;

import com.example.bakend.pojo.Admin;
import com.example.bakend.pojo.Result;
import com.example.bakend.service.AdminService;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    AdminService adminService;

//    修改密码
    @PutMapping
    public Result  updatePassword(@RequestBody Admin admin){
//        查询用户
        Admin ad = adminService.selectAdmin(admin);
        if (admin.getUsername().equals("") || admin.getPassword().equals("")){
            return Result.error("用户名或密码不能为空");
        }
//        用户不存在、存在
        if (ad == null){
            return Result.error("用户不存在");
        }else {
//            判断密码
            if (admin.getNewPassword().equals("")){
                return Result.error("新密码不能为空");
            }
            if (admin.getPassword().equals(admin.getNewPassword())){
                return Result.error("新密码不能和旧密码一样");
            }
            if(admin.getNewPassword().length() <=4){
                return Result.error("密码必须大于4位");
            }
        }
//        校验通过，修改密码
        Integer row = adminService.updatePassword(admin);
        return Result.success("修改成功");
    }

    @GetMapping
    public Result selectAdmin(@RequestBody Admin admin){
        return Result.success(adminService.selectAdmin(admin));
    }


}
